/*
 * @Author: 杨上漂
 * @Date: 2024-10-13 10:27:42
 * @LastEditors: 杨上漂
 * @LastEditTime: 2024-10-16 18:13:32
 * @FilePath: \gwlc\assets\scripts\FireBall.ts
 * @Description: 
 * 
 * Copyright (c) 2024 by ${git_name_email}, All Rights Reserved. 
 */
import { _decorator, BoxCollider2D, Collider, Collider2D, Component, Contact2DType, Director, director, dragonBones, find, ITriggerEvent, log, Node, RigidBody2D, TERRAIN_HEIGHT_BASE, v2, v3 } from 'cc';
import { playerSkillTypeEnum } from '../Enum';
import { Enemy } from '../mainScene/Enemy';
import { EnemyMgr } from '../mainScene/EnemyMgr';
import { PoolMgr } from '../PoolMgr';

const { ccclass, property } = _decorator;

@ccclass('Spike')
export class Spike extends Component {

    attackTargetPos=null;

    tempPos=v3();

    isAlreadyAttack=false;//地刺是否已经攻击过人

    init() {
        // this.poolReset();
        // this.node.getChildByName("sp").getComponent(dragonBones.ArmatureDisplay).playAnimation("Sprite",2)
        
        //获取目标敌人
        let attackArr = EnemyMgr.ins.getAnyExistFourEnemyNode()
        if (attackArr) {
            this.attackTargetPos = attackArr[Math.round(Math.random() * (attackArr.length - 1))].position;

        }
        else {
            //场上没敌人停止射击
        }

        this.node.setPosition(this.attackTargetPos.x,this.attackTargetPos.y-5);
        this.node.setParent(find("Canvas/bg/roundRoot"));

        let collider=this.node.getComponent(BoxCollider2D);
        collider.on(Contact2DType.BEGIN_CONTACT, this.onTriggerFuc, this);

        setTimeout(()=>{
            this.recycle();
        },700)
    }

    poolReset() {//重新设置，针对预制体池
        this.attackTargetPos = null;
        this.tempPos = v3();
        this.isAlreadyAttack = false;//地刺是否已经攻击过人
    }

    onTriggerFuc(self: Collider, other: Collider) {
        if (other.node.getComponent(BoxCollider2D).tag == 1) {
            director.once(Director.EVENT_AFTER_PHYSICS, () => {
                if (this.isAlreadyAttack == false) {
                    other.node.getComponent(Enemy).hurt(playerSkillTypeEnum.spike);
                    this.isAlreadyAttack = true;
                }
            }, this)
        }
    }

    recycle(){
        this.node.destroy();
        // PoolMgr.ins.poolPut(this.node.name,this.node);
    }
 
 
    
 
}


